<?php
/**
 * Created by PhpStorm.
 * User: TellGxx
 * Date: 2018/5/16
 * Time: 10:32
 */
namespace app\admin\model;

use think\Model;
class Caser extends Model{
    //获取用户表数据
    public function get_case(){
        $res = $this->table('d_case_content')
            ->field('*,a.id as a_id')
            ->alias('a')
            ->leftjoin ('d_case_cate b','a.catenum = b.id')
            ->leftjoin ('d_stage c','a.phase = c.id')
            ->select();
        return $res;
    }

    //获取用户表数据
    public function get_center($id){
        $res = $this->table('d_case_content')
            ->field('*,a.id as a_id')
            ->alias('a')
            ->join('d_case_cate b','a.catenum = b.id')
            ->join('d_stage c','a.phase = c.id')
            ->join('d_branch d','a.BOnum = d.id')
            ->where('a.id',$id)
            ->find();
        return $res;
    }


    //验证与处理添加用户信息
    public function get_adds($datas){
        if($datas['num']=='' || $datas['title']=='' || $datas['catenum']=='' || $datas['temp']=='' || $datas['phase']=='' || $datas['contacts']==''){
            return 999;
        }else{
            //session数据
            $sz1 = session('login.user_name');              //立案人
            $sz2 = session('login.id');                     //立案人ID
            //查询整理数据
            $sql1 = $this->table('d_user')->where('id',$datas['contacts'])->find();         //查询客户顾问信息
            $sql2 = $this->table('d_branch')->where('id',$datas['BOnum'])->find();          //查询分所信息
            $sql3 = $this->table('d_case_cate')->where('id',$datas['catenum'])->find();     //查询类型信息
            $sql4 = $this->table('d_customer')->where('id',$datas['client_code'])->find();  //查询客户信息
            //处理数据
            //(案件号码自动拼接)
            $row1 = strlen($datas['num']);
            if($row1==1){
                $row = '000'.$row1;
            }elseif($row1==2){
                $row = '00'.$row1;
            }elseif($row1==3){
                $row = '0'.$row1;
            }else{
                $row = $datas['num'];
            }
            //（整合案号）
            if($datas['outsidenum']==''){
                $row2 = $sql2['jc_name'].$row.'-'.$sql3['case_code'].'-'.$sql4['customer_case_number'];
            }else{
                $row2 = $sql2['jc_name'].$row.'-'.$sql3['case_code'].$datas['outsidenum'].'-'.$sql4['customer_case_number'];
            }
            //(重构优先权信息数组)
            $yxq = array();
            $ii = 0;
            foreach($datas['prioritydetail']["'country'"] as $k => $v){
                $yxq[$ii]['country'] = $v;
                $yxq[$ii]['times'] = $datas['prioritydetail']["'times'"][$ii];
                $yxq[$ii]['num'] = $datas['prioritydetail']["'num'"][$ii];
                $ii = $ii+1;
            }
            $yxqs = serialize($yxq);
            //(重构申请人信息数组)
            $sqr = array();
            $ii1 = 0;
            foreach($datas['applicantdetail']["'name'"] as $k => $v){
                $sqr[$ii1]['times'] = $datas['applicantdetail']["'name'"][$ii1];
                $sqr[$ii1]['num'] = $datas['applicantdetail']["'address'"][$ii1];
                $ii1 = $ii1+1;
            }
            $sqrs = serialize($sqr);
            //(重构发明人信息数组)
            $fmr = array();
            $ii2 = 0;
            foreach($datas['inventordetail']["'name'"] as $k => $v){
                $fmr[$ii2]['times'] = $datas['inventordetail']["'name'"][$ii2];
                $fmr[$ii2]['num'] = $datas['inventordetail']["'address'"][$ii2];
                $ii2 = $ii2+1;
            }
            $fmrs = serialize($fmr);
            //委托事宜
            $wtsy = serialize($datas['entrust']);
            //数据从组与整理
            $data['fieldnum'] = $datas['fieldnum'];         //申请号
            $data['fieldday'] = $datas['fieldday'];         //申请日
            $data['patentnum'] = $datas['patentnum'];       //专利号
            $data['num'] = $row;                            //案件号码
            $data['BOnum'] = $datas['BOnum'];               //案件所属分所ID
            $data['title'] = $datas['title'];               //案件名称
            $data['catenum'] = $datas['catenum'];           //案件类别编号
            $data['client_code'] = $datas['client_code'];   //客户ID（识别码）
            $data['contacts'] = $datas['contacts'];         //客户顾问ID
            $data['contacts_name'] = $sql1['user_name'];    //客户顾问名称
            $data['outsidenum'] = $datas['outsidenum'];     //外载案号
            $data['temp'] = $datas['temp'];                 //是否为临时案（1正式案，2临时案）
            $data['casefullnum'] = $row2;                   //整合案号
            $data['adder'] = $sz1;                          //立案人名称
            $data['adderid'] = $sz2;                        //立案人ID
            $data['phase'] = $datas['phase'];               //案件所在阶段
            $data['priority'] = $datas['priority'];         //优先权记录备注
            $data['prioritydetail'] = $yxqs;                //优先权详细信息
            $data['related'] = $datas['related'];          //关联案件记录
            $data['remark'] = $datas['remark'];             //备注
            $data['dateline'] = $datas['dateline'];         //立案时间
            $data['grantday'] = $datas['grantday'];         //授权日
            $data['publicdate'] = $datas['publicdate'];     //公开日
            $data['publicnum'] = $datas['publicnum'];       //公开号
            $data['applicant'] = $datas['applicant'];       //申请人备注信息
            $data['applicantdetail'] = $sqrs;               //申请人详细信息
            $data['inventor'] = $datas['inventor'];         //发明人备注信息
            $data['inventordetail'] = $fmrs;                //发明人详细信息
            $data['mothercase'] = $datas['mothercase'];     //母案
            $data['entrust'] = $wtsy;                       //委托事宜
            return $data;

        }
    }

    /**
     * @param string $grantday 年费缴纳截止日期
     * @param string $paynum 年费已缴纳次数 默认为1
     * @param string $payednum 年费需要缴纳的次数
     * @param string $y 年费缴纳间隔时间  year年 例如 间隔3.5年  即 3 年 6个月
     * @param string $m 年费缴纳间隔时间  month月
     * @param string $d 年费缴纳间隔时间  day 日
     * @param $redress          年费开始提醒日期矫正时间一般为6 个月
     * @return  false 则表示不需要提醒 true则返回一个提醒时间段
     */
    public function year_fee($grantday = '', $paynum = '', $payednum = '', $y = '', $m = '', $d = '', $redress = -6)
    {
        //缴纳过规定次之后就不需要缴纳
        if ($paynum >= $payednum) {
            return false;
        }
        //获取授权日时间戳
        $grantday = strtotime($grantday);
        //计算年费提醒时间
        $year = date('Y', $grantday);//年
        $month = date('m', $grantday);//月
        $day = date('d', $grantday);//日
        //开始提醒时间
        $r['firstfeedate'] = mktime(0, 0, 0, $month + $paynum * $m + $redress, $day + $paynum * $d, $year + $paynum * $y);
        //截止提醒时间 （过期超过一个月//将不再提醒）
        $r['lastfeedate'] = mktime(0, 0, 0, $month + $paynum * $m + 1, $day + $paynum * $d, $year + $paynum * $y);
//        echo date('Y-m-d',$r['firstfeedate']).'<br>';
//        echo date('Y-m-d',$r['lastfeedate']).'<br>';
        //获取当前时间戳
        $currentTime = time();
//        开始提醒时间小于当前时间and 当前时间小于过期一个月的时间 则表示应该提醒交年费了
        if ($r['firstfeedate'] <= $currentTime and $currentTime <= $r['lastfeedate']) {
            //返回一个提醒时间段
            return $r;
        } else {
            return false;
        }
    }



    //新建记录,重组数组
    public function get_cor($inputs,$arr){
        $res['recordm'] = $inputs['recordm'];
        $res['recorderid'] = session('login.id');
        $res['recordername'] = session('login.user_name');
        $res['caseid'] = $arr['a_id'];
        $res['casenum'] = $arr['num'];
        $res['casefullnum'] = $arr['casefullnum'];
        $res['casename'] = $arr['jc_name'].$arr['num'];
        $res['BOid'] = session('login.branch');
        $res['BOname'] = $arr['name'];
        $res['remark'] = $inputs['remark'];
        $res['phase'] = $arr['jd_name'];
        $res['dateline'] = time();
        $res['important'] = $inputs['important'];
        return $res;
    }
}